package com.jwt.Handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jwt.pojo.ResponseResult;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class MyAccessDeniedHandler implements AccessDeniedHandler {

	@Override
	public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
		response.setContentType("application/json;charset=utf-8");
		ResponseResult<?> result=new ResponseResult<>();
		result.setCode(3);
		result.setFlag(false);
		result.setMsg("权限不足");
		ObjectMapper objectMapper=new ObjectMapper();
		String s = objectMapper.writeValueAsString(result);
		response.getWriter().write(s);
	}
}
